<?php
class guestbookController extends AdminController{
	public function init()
	{
		$this->typename = '留言';
		$this->tableName = 'guestbook';
		parent::init();
	}

	public function addAction()
	{
		$this->assign('actionname', '添加' . $this->typename);
		$this->assign('categorylist', M('category')->getModuleList($this->module_id));
		parent::addAction();
	}
	public function insertAction()
	{
		$data = array();
		$data['email'] = $_POST['email'];
		$data['name'] = $_POST['name'];
		$data['siteurl'] = $_POST['siteurl'];
		$data['content'] = $_POST['content'];
		$data['status'] = 1;
		$data['datetime'] = strtotime($_POST['datetime']);
		if(!empty($_POST['admin_content'])){ 
			$data['admin_content'] = $_POST['admin_content'];
			$data['status'] = 2;
			$data['admin_datetime'] = strtotime($_POST['admin_datetime']);
		}
		$res=$this->model->insert($data);
		if ($res){
			$this->success('添加成功',cookie('admin_listurl'));
		}else{
			$this->error("添加失败[{$this->model->getLastSql()}]");
		}
	}
	public function editAction()
	{
		$id=I('get.id','en','');
		$info=$this->model->where(array($this->model->getPk()=>$id))->find();
		
		if ($info){
			$this->assign('actionname', '修改' . $this->typename);
			$this->assign('info',$info);
			$this->display();
		}else{
			$this->error('找不到指定的信息');
		}
	}
	public function updateAction()
	{
		$id=I('post.id','en','');
		$data = array();
		$data['email'] = $_POST['email'];
		$data['name'] = $_POST['name'];
		$data['siteurl'] = $_POST['siteurl'];
		$data['content'] = $_POST['content'];
		if(!empty($_POST['admin_content'])){ 
			$data['admin_content'] = $_POST['admin_content'];
			$data['status'] = 2;
			$data['admin_datetime'] = strtotime($_POST['admin_datetime']);
		}
		$res=$this->model->where(array($this->model->getPk()=>$id))->update($data);
		if ($res===0 or $res===1){
			
			$this->success('修改成功');
		}else{
			$this->error("修改失败[{$this->model->geterror()}]");
		}
	}
	/**
	 * 获取附加字段信息
	 * @return array
	 */
	public function getAddonData()
	{
		return array('content'=>$_POST['content']);
	}

	/**
	 * 解析内容
	 * @param $info
	 * @return mixed
	 */
	public function parseInfo($info)
	{
		return $info;
	}
}